package asteroids.model.programs.Statement;

import asteroids.model.programs.Program;
import asteroids.model.programs.Expression.Expression;
import asteroids.model.programs.Expression.IBooleanExpression;

/**
* 
* @version 0.3
* @author  Tandan Sunil, Van Deun Luca
*
*/
public class WhileLiteral extends LoopStatement {

	public WhileLiteral(int line, int column,Expression condition, Statement body) {
		super(line, column, body);
		if(!(condition instanceof IBooleanExpression))
			throw new IllegalArgumentException("Condition is not of type Bolean");
		this.condition = (IBooleanExpression) condition;
	}

	private IBooleanExpression condition;
	
	public IBooleanExpression getCondition(){
		return condition;
	}
	
	public boolean isConditionMet(){
		return condition.getBoolean();
	}

	private boolean executing;
	private boolean isExecuting(){
		return executing;
	}
	private void setExecuting(boolean executing){
		this.executing=executing;
	}
	@Override
	public void execute(Program program) {
		if(!executing && isConditionMet()){
			setExecuting(true);
			getBody().execute(program);
			if(getBody().isExecuted() && !isConditionMet()){
				setExecuted(true);
				setExecuting(false);
			}
			
		}
		else if(isExecuting()){
			getBody().execute(program);
			if(getBody().isExecuted() && !isConditionMet()){
				setExecuted(true);
				setExecuting(false);
			}
			
		}
		if(getBody().isExecuted() && isConditionMet()){
			setExecuted(false);
			setExecuting(false);
			getBody().setExecuted(false);
			execute(program);
		}
		
	}
}
